#!/usr/bin/env bash

set -u

readonly CUR_DIR="$(dirname $(realpath ${BASH_SOURCE[0]}))"
source ${CUR_DIR}/lib/lib.sh
readonly YEAR="$(date '+%Y')"
readonly MON="$(date '+%m')"
readonly TIMESTAMP="$(date '+%Y-%m-%d_%H_%M_%S')"
readonly LOG_DIR="${CUR_DIR}/log"
readonly FTP_DIR="${CUR_DIR}/ftp_root/${YEAR}"
readonly EXP_DIR="${CUR_DIR}/exp"
readonly LOG_FILE="${LOG_DIR}/ftp_log.txt"
readonly EXP_FILE="${EXP_DIR}/ftp_expect.exp"
readonly CONFIG_FILE="${CUR_DIR}/config/ftp.conf"

#check config file
#ftp.conf ftp.exp
cleanup(){
	rm -f ${CUR_DIR}/config_bk.lock
	exit 0
}
check_lock(){
	if [ -e ${CUR_DIR}/config_bk.lock ]; then
		echo "Duplicate Process!"
		exit 1
	fi
	touch ${CUR_DIR}/config_bk.lock
	trap cleanup EXIT INT TERM
}
check_config_file(){
	if ! [ -f ${CONFIG_FILE} ]; then
		echo "${CONFIG_FILE} no existed !"
		exit 1
	fi
}
#ftp_root
touch_dirs(){
	#mkdir
	mk_dir "${LOG_DIR}"
	if ! [ $? -eq 0 ]; then
		echo "make ${LOG_DIR} error"
		exit 1
	fi
	if ! [ -f ${LOG_FILE} ]; then
		touch "${LOG_FILE}"
		if ! [ $? -eq 0 ]; then
			echo "touch ${LOG_FILE} error"
			exit 1
		fi
	fi

	mk_dir "${FTP_DIR}"
	if ! [ $? -eq 0 ]; then
		log "${LOG_FILE}" "make ${FTP_DIR} error" 2
		exit 1
	fi
	#touch device_name dir
	while IFS=',' read -r device_name ip ftp_user ftp_passwd config_name;do
		DEVICE_DIR="${FTP_DIR}/${device_name}"
		if [[ ${device_name} =~ ^[[:space:]]*# ]];then
			continue
		fi
		device_name=$(echo "${device_name}" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//')
		if [ -z "${device_name}" ]; then
			continue
		fi
		mk_dir "${DEVICE_DIR}"
		if ! [ $? -eq 0 ]; then
			log "${LOG_FILE}" "make ${DEVICE_DIR} error" 2
			exit 1
		fi
		mk_dir "${DEVICE_DIR}/old"
		if ! [ $? -eq 0 ]; then
			log "${LOG_FILE}" "make ${DEVICE_DIR}/old error" 2
			exit 1
		fi
	done < ${CONFIG_FILE}
}

#read config file
download_config() {
	declare -i ret
	while IFS=',' read -r device_name ip ftp_user ftp_passwd config_name;do
		DEVICE_DIR="${FTP_DIR}/${device_name}"
		if [[ ${device_name} =~ ^[[:space:]]*# ]];then
			continue
		fi
		device_name=$(echo "${device_name}" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//')
		ip=$(echo "${ip}" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//')
		ftp_user=$(echo "${ftp_user}" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//')
		ftp_passwd=$(echo "${ftp_passwd}" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//')
		config_name=$(echo "${config_name}" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//')
		if [ -z "${device_name}" ]; then
			continue
		fi
		args=(${device_name} ${ip} ${ftp_user} ${ftp_passwd} ${DEVICE_DIR} ${config_name})
		#download config file
		expect "${EXP_FILE}" "${args[@]}"
		ret=$?
		# download successful
		if [ $ret -eq 0 ];then
			filename=$(ls ${DEVICE_DIR} | grep -E "${config_name}.*_current")
			if ! [ -z "${filename}" ]; then
				compare_file "${DEVICE_DIR}/${filename}" "${DEVICE_DIR}/${config_name}"
				if [ $? -eq 1 ];then
					new_name=$(echo "${filename}"| sed 's/current/deprecate/')
					mv "${DEVICE_DIR}/${filename}" "${DEVICE_DIR}/old/${new_name}"
					mv "${DEVICE_DIR}/${config_name}" "${DEVICE_DIR}/${config_name}_${TIMESTAMP}_current"
				else
					rm "${DEVICE_DIR}/${config_name}"
				fi
			else
				mv "${DEVICE_DIR}/${config_name}" "${DEVICE_DIR}/${config_name}_${TIMESTAMP}_current"
			fi
		# download failed
		else
			log "${LOG_FILE}" "Download ${device_name} ${config_name} failed" 1
			touch "${DEVICE_DIR}/ERROR_"${TIMESTAMP}
		fi
	done < ${CONFIG_FILE}
}

main() {
	check_lock
	sleep 20
	touch_dirs
	check_config_file
	echo "开始备份设备配置 $(date '+%Y-%m-%d %H:%M:%S')"
	log "${LOG_FILE}" "开始备份" 0
	download_config
	echo "结束备份设备配置 $(date '+%Y-%m-%d %H:%M:%S')"
	log "${LOG_FILE}" "结束备份" 0
}

main "$@"
